﻿using OfficeOpenXml;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MakeProcessExcelTables
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var paras = ExcelHelper.ExcelToData.GetData<ProcessBase.Models.Parameter>(@"E:\temp\数据库更新\工序参数及记录.xlsx", "Parameters");

                var procs = paras.Select(x => x.ProcessName).Distinct().OrderBy(x => x);

                string dir = @"E:\temp\数据库更新\生成的工序表";
                Directory.CreateDirectory(dir);
                int procIndex = 1;
                foreach (var proc in procs)
                {
                    string fileName = Path.Combine(dir, $"{proc}.xlsx");
                    
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }

                    ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                    using (var package = new ExcelPackage(new FileInfo(fileName)))
                    {
                        var currentparas = paras.Where(x => x.ProcessName == proc).OrderBy(x => x.OrderID);


                        //添加表
                        var sheet = package.Workbook.Worksheets.Add(proc);

                        //
                        sheet.Cells.Style.ShrinkToFit = true;//这个在调整单元格时自动调整字体大小
                        



                        //添加头部
                        sheet.Cells[1, 1].Value = $"单片电容器基板生产工序记录-【{proc}】";
                        sheet.Cells[2, 1].Value = $"  QG/PWV.C11-{5000+procIndex++:0000}-A/0";

                        //
                        int col = 1;
                        foreach(var para in currentparas)
                        {
                            //表头
                            string title = para.ParameterName;
                            sheet.Cells[3, col].Value = title;//==

                            if (!string.IsNullOrWhiteSpace(para.ParameterUnit))
                            {
                                title = para.ParameterUnit;
                                sheet.Cells[4, col].Value = title;//==
                            }
                            else
                            {
                                //合并单元格
                                sheet.Cells[3, col, 4, col].Merge = true;
                                sheet.Cells[3, col, 4, col].Style.VerticalAlignment = ExcelVerticalAlignment.Top;
                                sheet.Cells[3, col, 4, col].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                            }
                           
                            
                            //默认值
                            switch (para.ParameterType)
                            {                                
                                case ProcessBase.Models.ParameterType.Number:
                                    if (para.DefaultNumberValue != null&&!para.IsCheckPoint)
                                    {
                                        //sheet.Cells[5, col].Value =$"{para.DefaultNumberValue.Value.ToString()}【   】" ;//==

                                        for(int i = 5; i < 100; i++)
                                        {
                                            sheet.Cells[i, col].Value = $"{para.DefaultNumberValue.Value.ToString()}【   】";//==
                                        }
                                    }
                                    break;
                                case ProcessBase.Models.ParameterType.String:
                                    if (!string.IsNullOrEmpty(para.DefaultStringValue)&&!para.IsCheckPoint)
                                    {
                                        if (!para.Category.Contains("操作员") && !para.Category.Contains("监配人"))
                                        {
                                            //sheet.Cells[5, col].Value = $"{para.DefaultStringValue}【   】";//== 
                                            for (int i = 5; i < 100; i++)
                                            {
                                                sheet.Cells[i, col].Value = $"{para.DefaultStringValue}【   】";//== 
                                            }
                                        }
                                    }
                                    break;
                                //case ProcessBase.Models.ParameterType.Date:
                                //    if (para.DefaultDateTimeValue != null)
                                //    {
                                //        sheet.Cells[5, col].Value = para.DefaultDateTimeValue.Value.ToString("yyyy/MM/dd");//==
                                //    }                                    
                                //    break;
                                //case ProcessBase.Models.ParameterType.Time:
                                //    if (para.DefaultDateTimeValue != null)
                                //    {
                                //        sheet.Cells[5, col].Value = para.DefaultDateTimeValue.Value.ToString("yyyy/MM/dd hh:mm:ss");//==
                                //    }                                    
                                //    break;
                                default:
                                                            
                                    break;
                                   
                            }
                            col++;

                            if (para.IsCheckPoint)//检验项
                            {
                                if (string.IsNullOrWhiteSpace(para.ParameterUnit))
                                {                                   
                                    sheet.Cells[4, col - 1].Value = "参数值";//==
                                }                                

                                title = para.ParameterName;
                                sheet.Cells[3, col].Value = title;//==

                                title = "合格率";
                                sheet.Cells[4, col].Value = title;//==
                                col++;

                                title = para.ParameterName;
                                sheet.Cells[3, col].Value = title;//==
                                title = "检验员";
                                sheet.Cells[4, col].Value = title;//==
                                col++;

                                title = para.ParameterName;
                                sheet.Cells[3, col].Value = title;//==
                                title = "检验结论";
                                sheet.Cells[4, col].Value = title;//==
                                col++;
                            }                
                        }

                        //合并单元格
                        sheet.Cells[1, 1, 1, col-1].Merge = true;
                        sheet.Cells[1, 1, 1, col-1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        sheet.Cells[1, 1, 1, col - 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

                        sheet.Cells[2, 1, 2, col - 1].Merge = true;
                        sheet.Cells[2, 1, 2, col - 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                        sheet.Cells[2, 1, 2, col - 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;

                        //边框
                        sheet.Cells.Style.Border.Top.Style = ExcelBorderStyle.Medium;
                        sheet.Cells.Style.Border.Right.Style = ExcelBorderStyle.Medium;
                        sheet.Cells.Style.Border.Bottom.Style = ExcelBorderStyle.Medium;
                        sheet.Cells.Style.Border.Left.Style = ExcelBorderStyle.Medium;

                        //纸张横向
                        sheet.PrinterSettings.Orientation = eOrientation.Landscape;

                        //页边距
                        sheet.PrinterSettings.TopMargin = 1.0M / 2.54M;
                        sheet.PrinterSettings.BottomMargin = 1.0M / 2.54M;
                        sheet.PrinterSettings.LeftMargin = 0.64M / 2.54M;
                        sheet.PrinterSettings.RightMargin = 0.64M / 2.54M;

                        


                        package.Save();
                    }

                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
                return;
            }
            Console.WriteLine("完成！");
            Console.ReadKey();
        }
    }
}
